package com.UI;

import com.Model.StudentDBModel;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class AddClassEvent extends Activity{
	private String eventType;
	private String courseNo;
	private String comboID;
	 @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();
		courseNo = extras.getString("CourseNum");
		
		// create a model instance
		final StudentDBModel model = new StudentDBModel(this);
		
		setContentView(R.layout.add_class_event);
		
		//create the spinner to hold the list of Class events that can be added
		Spinner classEventSpinner = (Spinner) findViewById(R.id.addClassEventSpinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.AddClassEventArray, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		classEventSpinner.setAdapter(adapter);

		// Set up a call back for the spinner
		classEventSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onNothingSelected(AdapterView<?> arg0) {
				// Do nothing.
			}

			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				final Spinner selectedEvent = (Spinner) findViewById(R.id.addClassEventSpinner);
				eventType = selectedEvent.getSelectedItem().toString();
			}

		});
		
		// register this activity as a button event listener
		Button btnSubmit = (Button) findViewById(R.id.btnClassEventSubmit);
		btnSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
		 		EditText txtClassEName = (EditText)findViewById(R.id.txtClassEventName);
		 		
				String tempClassEventName = txtClassEName.getText().toString().replaceAll(" ", "");
				
				if(tempClassEventName.length() == 0) {
		  	      
		 		   AlertDialog alertDialog = new AlertDialog.Builder(AddClassEvent.this).create();	        			
		 		   alertDialog.setTitle("Warning");	        		  
		 		   alertDialog.setMessage("Please Enter a Class Event Name");	        		  	        		  
		 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			 		   public void onClick(DialogInterface dialog, int which) {
					       return;
			 		   } 
		 		   }); 
		 		   alertDialog.show();  
				}
				else if(eventType.length() == 0){
			 		   AlertDialog alertDialog = new AlertDialog.Builder(AddClassEvent.this).create();	        			
			 		   alertDialog.setTitle("Warning");	        		  
			 		   alertDialog.setMessage("Please select a Class Event Type");	        		  	        		  
			 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				 		  public void onClick(DialogInterface dialog, int which) {
						       return;
						  } 
			 		   }); 
			 		   alertDialog.show();
				}
				else {
					// Get the values from the user and send them to the DB
					ContentValues values = new ContentValues();
					EditText txtClassEventName = (EditText) findViewById(R.id.txtClassEventName);
					
					comboID = courseNo + " - " + txtClassEventName.getText().toString();
					
					values.put("EventCourseComboId", comboID);
					
					values.put("EventName", txtClassEventName.getText().toString());
	
					values.put("CourseId", courseNo);
	
					values.put("EventType", eventType);
	
					EditText txtClassEventWeight = (EditText) findViewById(R.id.txtClassEventWeight);
					values.put("EventWeight", txtClassEventWeight.getText().toString());
					
					EditText txtClassEventDueDate = (EditText) findViewById(R.id.txtClassEventDueDate);
					values.put("DueDate", txtClassEventDueDate.getText().toString());
					
					EditText txtMarkReceived = (EditText) findViewById(R.id.txtMarkReceived);				
					if (txtMarkReceived.getText().toString().equals("")){
						values.put("MarkReceived", 0);
					}
					else {
						values.put("MarkReceived", txtMarkReceived.getText().toString());
					}							
					
					EditText txtOutOf = (EditText) findViewById(R.id.txtOutOf);	
					if (txtOutOf.getText().toString().equals("")){
						values.put("OutOf", 0);
					}
					else {
						values.put("OutOf", txtOutOf.getText().toString());
					}
					
					// use the model to store the data
					model.addNewClassEvent(values);
	
					// tell the user the assignment was added to the database
					int numClassEvents = model.getNumClassEvents();
					TextView lblResults = (TextView) findViewById(R.id.lblAddClassEventResult);
					lblResults.setText(numClassEvents + getString(R.string.classEventAdded));
					
					//finish the activity to return to the calling event
					AddClassEvent.this.finish();
				}
			}
		});
	
		// register this activity as a button event listener
		Button btnGoBack = (Button) findViewById(R.id.btnBack);
		btnGoBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				AddClassEvent.this.finish();
			}
		});
		
		Button btnGoHome = (Button) findViewById(R.id.btnHome);
		btnGoHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent goHome = new Intent(AddClassEvent.this, HomePage.class);
				startActivity(goHome);
				//finish the activity to return to the calling event
				AddClassEvent.this.finish();
			}
		});
	}
}
